
const Router = require('@koa/router');
const { searchMovieByText, getMovieInfoById,getMovieCelebritiesById, getCelebrityInfoById,getCelebrityPhotoById } = require('../search/douban_movie');
const movie_router = new Router({ prefix: '/movie' });



// 电影演员搜索
movie_router.get('/search', async (ctx) => {
  const { text,start } = ctx.query;
  if (!text || text === '') {
    ctx.status = 400;
    ctx.body = {
      success: false,
      message: 'text is required.',
    };
    return;
  } 
  try {
    const result = await searchMovieByText(text,start);
    ctx.body = {
      success: true,
      data: result,
    };
  } catch (err) {
    console.log(err);
    ctx.status = 500;
    ctx.body = {
      success: false,
      message: err,
    };
  }
});

// 电影详情
movie_router.get('/subject/:id', async (ctx) => {
  const id = ctx.params.id;
  try {
    const info = await getMovieInfoById(id);
    ctx.body = {
      success: true,
      data: info
    };
  } catch (err) {
    console.log(err);
    ctx.response.status = 500;
    ctx.body = {
      success: false,
      message: err,
    };
  }
});

// 电影所有演员列表
movie_router.get('/subject/:id/celebrities', async (ctx) => {
  const id = ctx.params.id;
  try {
    const info = await getMovieCelebritiesById(id);
    ctx.body = {
      success: true,
      data: info
    };
  } catch (err) {
    console.log(err);
    ctx.response.status = 500;
    ctx.body = {
      success: false,
      message: err,
    };
  }
});




// 演员详情
movie_router.get('/celebrity/:id', async (ctx) => {
  const subjectId = ctx.params.id;
  try {
    const info = await getCelebrityInfoById(subjectId);
    ctx.body = {
      success: true,
      data: info,
    };
  } catch (err) {
    console.log(err);
    ctx.response.status = 500;
    ctx.body = {
      success: false,
      message: err,
    };
  }
});

// 演员图库
movie_router.get('/celebrity/:id/photos', async (ctx) => {
  const subjectId = ctx.params.id;
  try {
    const info = await getCelebrityPhotoById(subjectId);
    ctx.body = {
      success: true,
      data: info,
    };
  } catch (err) {
    console.log(err);
    ctx.response.status = 500;
    ctx.body = {
      success: false,
      message: err,
    };
  }
});


module.exports = {
  movie_router
};
